﻿using Microsoft.Web.WebView2.Core;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Shell;
using WebView2.Model;
using WebView2.ViewModels;

namespace WebView2.Views
{
    /// <summary>
    /// BrowserTabView.xaml 的交互逻辑
    /// </summary>
    public partial class BrowserTabView : UserControl
    {

        private BrowserTabViewModel browserTabViewModel;

        public BrowserTabView()
        {
            InitializeComponent();
            webView.SourceChanged += WebView_SourceChanged;
            webView.CoreWebView2Ready += WebView_CoreWebView2Ready;
        }

        private void WebView_CoreWebView2Ready(object sender, EventArgs e)
        {
            webView.CoreWebView2.NewWindowRequested += CoreWebView2_NewWindowRequested;
            webView.CoreWebView2.HistoryChanged += CoreWebView2_HistoryChanged;
        }

        private void CoreWebView2_HistoryChanged(object sender, object e)
        {
          
        }

        /// <summary>
        /// 打开新窗口事件(使用选项卡打开)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CoreWebView2_NewWindowRequested(object sender, CoreWebView2NewWindowRequestedEventArgs e)
        {

            //MessageBox.Show(e.Uri);
            e.Handled = true;
            var mian = Application.Current.MainWindow as MainWindow;
            mian.OpenNewTab(e.Uri);
        }

        private void WebView_SourceChanged(object sender, CoreWebView2SourceChangedEventArgs e)
        {
            if (browserTabViewModel != null)
            {
                browserTabViewModel.AddressEditable = webView.Source.AbsoluteUri;
            }
        }

        private bool _isNavigating = false;


        private void OnTextBoxGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            var textBox = (TextBox)sender;
            textBox.SelectAll();
        }

        private void OnTextBoxGotMouseCapture(object sender, MouseEventArgs e)
        {
            var textBox = (TextBox)sender;
            textBox.SelectAll();
        }

        void WebView_NavigationStarting(object sender, CoreWebView2NavigationStartingEventArgs e)
        {
            _isNavigating = true;
            if (browserTabViewModel != null)
            {
                browserTabViewModel.IsNavigating = _isNavigating;
                browserTabViewModel.UpdateBrowseRaiseCanExecuteChanged();//刷新按钮状态
            }
        }

        void WebView_NavigationCompleted(object sender, CoreWebView2NavigationCompletedEventArgs e)
        {
            _isNavigating = false;
            if (browserTabViewModel != null)
            {
                var title = webView.CoreWebView2.DocumentTitle;
                browserTabViewModel.Title = title;
                browserTabViewModel.IsNavigating = _isNavigating;
                browserTabViewModel.UpdateBrowseRaiseCanExecuteChanged();//刷新按钮状态
            }
            var id = webView.CoreWebView2.BrowserProcessId;
            var documentTitle = webView.CoreWebView2.DocumentTitle;
            var data = new History()
            {
                URL = webView.Source.AbsoluteUri,
                DocumentTitle = documentTitle,
                BrowserProcessId = (int)id
            };
            DB.fsql.Insert(data).ExecuteAffrows();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
           var vm =  this.DataContext as BrowserTabViewModel;
            if (vm!=null)
            {
                browserTabViewModel = vm;
                vm.webView = this.webView;
            }
        }
    }
}
